﻿using System;
using System.Collections.Generic;
using Csla;
using System.Data;
using ENT.GP.Datos.MysqlHelper;
using MySql.Data.MySqlClient;

namespace ENT.GP.Core.Listas
{
    [Serializable]
    public class ProvinciaLista :
      ReadOnlyListBase<ProvinciaLista, ProvinciaInfo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(ProvinciaLista), "Role");
        }

        #endregion

        #region Factory Methods

        public static ProvinciaLista GetProvinciaLista()
        {
            return DataPortal.Fetch<ProvinciaLista>();
        }

        private ProvinciaLista()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch()
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            MySqlParameter[] parametros = new MySqlParameter[2];
            parametros[0] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO", "PROVINCIAR");
            parametros[1] = new MySql.Data.MySqlClient.MySqlParameter("CODIGO2", "DEPARTAMENTOP");
            foreach (DataRow items in objhm.EjecutarStoreProcedure(parametros, "Listar_Catalogo_Codigos").Tables[0].Rows)
            {
                this.Add(ProvinciaInfo.GetProvinciaInfo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class ProvinciaInfo : ReadOnlyBase<ProvinciaInfo>
    {
        #region Business Methods

        public int Id { get; set; }
        public int IdCatalogoPadre { get; set; }
        public string Codigo { get; set; }
        public string Nombre { get; set; }
        public string Descripcion { get; set; }

        #endregion

        #region Factory Methods

        internal static ProvinciaInfo GetProvinciaInfo(DataRow childData)
        {
            return DataPortal.FetchChild<ProvinciaInfo>(childData);
        }

        private ProvinciaInfo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            int id, idp;
            int.TryParse(childData[0].ToString(), out id);
            Id = id;
            Codigo = childData[1].ToString();
            Nombre = childData[2].ToString();
            Descripcion = childData[3].ToString();
            int.TryParse(childData[4].ToString(), out idp);
            IdCatalogoPadre = idp;
        }

        #endregion
    }
}
